home *** CD-ROM | disk | FTP | other *** search
/ CU Amiga Super CD-ROM 22 / CU Amiga Magazine's Super CD-ROM 22 (1998)(EMAP Images)(GB)[!][issue 1998-05].iso / PowerPC / Programming / PPCsiod / sources / io2.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-09-22  |  7.3 KB  |  286 lines

  1. /* Scheme In One Define.
  2.  
  3. The garbage collector, the name and other parts of this program are
  4.  
  5.  *                     COPYRIGHT (c) 1989 BY                              *
  6.  *      PARADIGM ASSOCIATES INCORPORATED, CAMBRIDGE, MASSACHUSETTS.       *
  7.  
  8. Conversion  to  full scheme standard, characters, vectors, ports, complex &
  9. rational numbers, and other major enhancments by
  10.  
  11.  *      Scaglione Ermanno, v. Pirinoli 16 IMPERIA P.M. 18100 ITALY        * 
  12.  
  13. Permission  to use, copy, modify, distribute and sell this software and its
  14. documentation  for  any purpose and without fee is hereby granted, provided
  15. that  the  above  copyright  notice appear in all copies and that both that
  16. copyright   notice   and   this  permission  notice  appear  in  supporting
  17. documentation,  and that the name of Paradigm Associates Inc not be used in
  18. advertising or publicity pertaining to distribution of the software without
  19. specific, written prior permission.
  20.  
  21. PARADIGM  DISCLAIMS  ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
  22. ALL  IMPLIED  WARRANTIES  OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL
  23. PARADIGM  BE  LIABLE  FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR
  24. ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER
  25. IN  AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
  26. OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  27.  
  28. */
  29.  
  30. #include <stdio.h>
  31. #include <string.h>
  32. #include <ctype.h>
  33. #include <setjmp.h>
  34. #include <signal.h>
  35. #include <math.h>
  36. #include <errno.h>
  37. #include <limits.h>
  38.  
  39. #include "siod.h"
  40.  
  41. LISP lread(LISP port)
  42. {FILE *f;
  43.  if(NULLP(port))
  44.   {f = get_cur_in();
  45.    return(lreadf(f));}
  46.  else
  47.   {if(NPORTP(port)) err("read",port,ERR_FIRST | ERR_NPOR);
  48.    f = PORTPTR(port);
  49.    return(lreadf(f));}}
  50.  
  51. static int ung_flag = 1;
  52.  
  53. int f_getc(FILE *f)
  54. {long iflag;
  55.  int c;
  56.  iflag = no_interrupt(1);
  57.  c = getc(f);
  58.  if(transfile)
  59.   {if(ung_flag) 
  60.      putc(c,transfile);
  61.    else
  62.      ung_flag=1;}
  63.  no_interrupt(iflag);
  64.  return(c);}
  65.  
  66. void f_ungetc(int c, FILE *f)
  67. {long flag;
  68.  flag = no_interrupt(1);
  69.  ungetc(c,f);
  70.  ung_flag=0;
  71.  no_interrupt(flag);}
  72.  
  73. int flush_ws(FILE *f,char *eoferr)
  74. {int c,commentp;
  75.  commentp = 0;
  76.  while(1)
  77.    {c = GETC_FCN(f);
  78.     if (c == EOF) if (eoferr) err(eoferr,NIL,ERR_GEN); else return(c);
  79.     if (commentp) {if (c == '\n') commentp = 0;}
  80.     else if (c == ';') commentp = 1;
  81.     else if (!isspace(c)) return(c);}}
  82.  
  83. LISP lreadchar(LISP port)
  84. {FILE *f;
  85.  int ch;
  86.  if(NULLP(port))
  87.   {f = get_cur_in();
  88.    ch = f_getc(f);}
  89.  else
  90.   {if(NPORTP(port)) err("read-char",port,ERR_FIRST | ERR_NPOR);
  91.    f = PORTPTR(port);
  92.    ch = f_getc(f);}
  93.  return((ch == EOF) ? eof_val : charcons(ch));}
  94.  
  95. LISP lflushinput(LISP port)
  96. {FILE *f;
  97.  int ch;
  98.  if(NULLP(port))
  99.   {f = get_cur_in();
  100.    while(((ch=f_getc(f))!='\n')&&(ch!=EOF));}
  101.  else
  102.   {if(NPORTP(port)) err("flush-input",port,ERR_FIRST | ERR_NPOR);
  103.    f = PORTPTR(port);
  104.    while(((ch=f_getc(f))!='\n')&&(ch!=EOF));}
  105.  return(NIL);}
  106.  
  107. LISP f_getstr(FILE *f)
  108. {char c,*p;
  109.  LISP s;
  110.  int j;
  111.  c = f_getc(f);
  112.  if (c == EOF) return(eof_val);
  113.  p = tkbuffer;
  114.  *p++ = c;
  115.  for(j = 1; j<TKBUFFERN; ++j)
  116.    {c = f_getc(f);
  117.     if ((c == EOF) || (c == '\n'))
  118.       {*p = '\0';
  119.        s=strcons(j+1);
  120.        strcpy(SNAME(s),tkbuffer);
  121.        return(s);}
  122.     *p++ = c;}
  123.  err("line larger than TKBUFFERN",NIL,ERR_GEN);}
  124.  
  125. LISP lreadline(LISP port)
  126. {FILE *f;
  127.  if(NULLP(port))
  128.   {f = get_cur_in();
  129.    return(f_getstr(f));}
  130.  else
  131.   {if(NPORTP(port)) err("read-line",port,ERR_FIRST | ERR_NPOR);
  132.    f = PORTPTR(port);
  133.    return(f_getstr(f));}}
  134.  
  135. LISP lreadf(FILE *f)
  136. {int c;
  137.  c = flush_ws(f,(char *)NULL);
  138.  if (c == EOF) return(eof_val);
  139.  UNGETC_FCN(c,f);
  140.  return(lreadr(f));}
  141.  
  142. LISP lreadr(FILE *f)
  143. {int c,j,d;
  144.  char *p,*u;
  145.  LISP s;
  146.  p = tkbuffer;
  147.  c = flush_ws(f,"end of file inside read");
  148.  switch (c)
  149.    {case '(':
  150.       return(lreadparen(f));
  151.     case ')':
  152.       err("unexpected close paren",NIL,ERR_GEN);
  153.     case '\'':
  154.       return(cons(sym_quote,cons(lreadr(f),NIL)));
  155.     case '\\':
  156.       *p++ = GETC_FCN(f);
  157.       *p='\0';
  158.       return(rintern(tkbuffer));
  159.     case '#':
  160.       {*p++ = c;
  161.        c = GETC_FCN(f);
  162.        if(c=='(')
  163.          return(listtovector(lreadparen(f)));
  164.        if(c=='\\')
  165.         {*p++= c;
  166.          *p =  GETC_FCN(f);
  167.          c =  GETC_FCN(f);
  168.          if (c == EOF) return(charcons(*p));
  169.          if ((isspace(c)) || (strchr("()'`,;\"",c)))
  170.             {UNGETC_FCN(c,f);return(charcons(*p));}
  171.          p++;}
  172.        break;}
  173.      case '`':
  174.       return(cons(cintern("quasiquote"),cons(lreadr(f),NIL)));
  175.     case '\"':
  176.     case '|':
  177.       {j = 0;
  178.        while(((d = GETC_FCN(f)) != c) && (d != EOF))
  179.          {if ((j + 2) > TKBUFFERN) err("string or slashed symbol larger than token buffer",NIL,ERR_GEN);
  180.           if(d=='\\')
  181.             d = GETC_FCN(f);
  182.           *(tkbuffer+j) = d;
  183.           ++j;}
  184.        *(tkbuffer+j) = 0;
  185.        if(c == '\"')
  186.          {s = strcons(j+1);
  187.           strcpy(SNAME(s),tkbuffer);
  188.           return(s);}
  189.        return(rintern(tkbuffer));}
  190.     case ',':
  191.       c = GETC_FCN(f);
  192.       switch(c)
  193.     {case '@':
  194.        u = "unquote-splicing";
  195.        break;
  196.      case '.':
  197.        u = "+internal-comma-dot";
  198.        break;
  199.      default:
  200.        u = "unquote";
  201.        UNGETC_FCN(c,f);}
  202.       return(cons(cintern(u),cons(lreadr(f),NIL)));}
  203.  *p++ = c;
  204.  for(j = 1; j<TKBUFFERN; ++j)
  205.    {c = GETC_FCN(f);
  206.     if (c == EOF) {*p='\0';return(lreadtk(1));}
  207.     if ((isspace(c)) || (strchr("()'`,;\"",c)))
  208.       {UNGETC_FCN(c,f);*p='\0';return(lreadtk(1));}
  209.     *p++ = c;}
  210.  err("token larger than token buffer",NIL,ERR_GEN);}
  211.  
  212. LISP lreadparen(FILE *f)
  213. {int c;
  214.  LISP tmp;
  215.  c = flush_ws(f,"end of file inside list");
  216.  if (c == ')') return(NIL);
  217.  UNGETC_FCN(c,f);
  218.  tmp = lreadr(f);
  219.  if EQ(tmp,sym_dot)
  220.    {tmp = lreadr(f);
  221.     c = flush_ws(f,"end of file inside list");
  222.     if (c != ')') err("missing close paren",NIL,ERR_GEN);
  223.     return(tmp);}
  224.  return(cons(tmp,lreadparen(f)));}
  225.  
  226. LISP lreadtk(long flag)
  227. {char *p,*r,*t;
  228.  int adigit;
  229.  double res1,res2,tst;
  230.  p = tkbuffer;
  231.  if(*p=='\#')
  232.   {p++;
  233.    if(tolower(*p)=='t')
  234.      {p++;
  235.       if(*p=='\0')
  236.         return(flag?truth:NIL);}         
  237.    if(tolower(*p)=='f')
  238.       {p++;
  239.        if(*p=='\0')
  240.           return(NIL);}
  241.    goto a_symbol;}         
  242.  res1=strtod(p,&r);
  243.  if(*r=='\0')
  244.    {if(isdigit(*(r-1)))
  245.        return(flag?flocons(res1):NIL);
  246.    else
  247.        r--;}  
  248.  if((tolower(*r)=='i')&&(*(r+1)=='\0')&&(r!=p))
  249.    return(flag?compcons((float)0,(float)res1):NIL);
  250.  if((*r=='+')||(*r=='-'))
  251.    {res2=strtod(r,&t);
  252.     if((tolower(*t)=='i')&&(*(t+1)=='\0'))
  253.         return(flag?compcons((float)res1,(float)res2):NIL);
  254.     if((tolower(*(r+1))=='i')&&(*(r+2)=='\0'))
  255.         if(*r=='+')
  256.            return(flag?compcons((float)res1,(float)1):NIL);
  257.         else
  258.            return(flag?compcons((float)res1,(float)-1):NIL);}
  259.  if((*r=='/')&&(r!=p))
  260.    {res2=strtod(r+1,&t);
  261.     if((*t=='\0')&&(res2>=0))
  262.       if((modf(res1,&tst) == 0.) && (modf(res2,&tst) == 0.))
  263.          return(flag?ratcons(res1,res2):NIL);}
  264.  a_symbol:
  265.  if(flag)
  266.   {p=tkbuffer;
  267.    while(*p)
  268.      {*p=tolower(*p);
  269.       p++;}
  270.    return(rintern(tkbuffer));}
  271.   else
  272.    return(truth);}
  273.  
  274. LISP transon(LISP name)
  275. {if NSTRINGP(name) err("transcript-on",name,ERR_GEN_ARG | ERR_NSTR);
  276.  transfile=fopen(SNAME(name),"a");
  277.  if(transfile==NULL) err("could not open transcript file",name,ERR_GEN);
  278.  return(truth);}
  279.  
  280. LISP transoff(void)
  281. {if(transfile) 
  282.   {if(fclose(transfile)==EOF) err("could not close transcript file",NIL,ERR_GEN);
  283.    transfile=NULL;
  284.    return(truth);}
  285.  return(NIL);}
  286.